package controller

import (
	"git.mycaigou.com/gfyx/common/authorize"
	contract2 "git.mycaigou.com/gfyx/micro-gfyx-bigdata-service/service/bigdata/contract"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type AuthController struct {
	BaseController
	writeService contract2.WriteServiceIface
}

func NewAuthController(writeService contract2.WriteServiceIface) *AuthController {
	return &AuthController{
		writeService: writeService,
	}
}

func (a *AuthController) RegisterRouter(g *gin.RouterGroup) {
	g.GET("/get-token", a.GetToken)
}

// 获取token
func (a *AuthController) GetToken(ctx *gin.Context) {
	appId := ctx.Query("app_id")
	appKey := stark.Configer.(stark.ApolloConfigerIface).GetStringValue("Auth."+appId+".AppKey", "")
	if appKey == "" || appKey != ctx.Query("app_key") {
		a.Fail(ctx, -1, "app_id或app_key不正确")
		return
	}
	jwtToken, err := authorize.GenerateServiceAuthToken(appId, "", appKey)
	if err != nil {
		a.Fail(ctx, -1, err.Error())
		return
	}

	a.Success(ctx, jwtToken)
}
